package com.zx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.entity.EHetong;
import com.zx.entity.EHetongdetail;
import com.zx.entity.HetongAll;
import com.zx.mapper.EHetongMapper;
import com.zx.mapper.EHetongdetailMapper;
import com.zx.service.IEHetongService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhouxin
 * @since 2020-06-10
 */
@Service
public class EHetongServiceImpl extends ServiceImpl<EHetongMapper, EHetong> implements IEHetongService {

    @Autowired
    private EHetongMapper hetongMapper;

    @Autowired
    private EHetongdetailMapper hetongdetailMapper;

    @Override
    public void addAll(HetongAll hetongAll) {
        if (hetongAll.getHetong().getId()!=null){
            //当id不为空的时候是修改操作
            hetongMapper.updateById(hetongAll.getHetong());
        }else{
            //当id为空时是添加操作
            hetongMapper.addHetong(hetongAll.getHetong());
        }
        //下面是子表的数据 一对多 先删除再添加
        List<EHetongdetail> list = hetongAll.getHetongdetailList();
        //进行多条件查询查询hid=合同id的数据进行删除
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("hid",hetongAll.getHetong().getId());
        hetongdetailMapper.delete(queryWrapper);
        //进行for循环添加
        for (EHetongdetail hetongdetail:list) {
            hetongdetail.setHid(hetongAll.getHetong().getId());
            hetongdetailMapper.insert(hetongdetail);
        }
    }
}
